<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @date-change="dateChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.taskstation_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/ems/taskstation";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          dateBtn:true,
          searchIndex:3,
          searchIcon:true,  
          defaultSort:{
            prop: 'driverTime',
            order: 'descending'
          },                   
          column: [
            {
              label: "任务号",
              prop: "taskId",
              hide: true,
              width: 90,
              editable: false, 
              rules: [{
                required: true,
                message: "请输入任务ID",
                trigger: "blur"
              }]
            },
            {
              label: "线路",
              prop: "lineName",
              width: 150,
              search:true,
              sortable:true,
              editable: false, 
            },
            {
              label: "站点ID",
              prop: "stationId",
              hide: true,
              rules: [{
                required: true,
                message: "请输入站点ID",
                trigger: "blur"
              }]
            },
            {
              label: "站点名称",
              prop: "name",
              search:true,
              rules: [{
                required: false,
                message: "请输入站点名称",
                trigger: "blur"
              }]
            },
            {
              label: "站点类型",
              prop: "type",
              type: "select",
              search:true,
              searchOrder: 3,
              sortable:true,
              width: 100,
              dicData: [
                {
                  label: "首发站",
                  value: 1
                },
                {
                  label: "途经站",
                  value: 2
                },
                {
                  label: "末站",
                  value: 3
                },
              ],                 
              rules: [{
                required: true,
                message: "请输入站点类型(1-首站 2-途经站 3-末站）",
                trigger: "blur"
              }]
            },
            {
              label: "顺序",
              prop: "sort",
              sortable:true,
              width: 90,
              tip: "按站点从小到大的顺序展示路线站点",
              rules: [{
                required: true,
                message: "请输入站点排序(用于线路默认导航顺序）",
                trigger: "blur"
              }]
            },
            {
              label: "签到时间",
              prop: "driverTime",
              search:true,
              sortable:true,
              searchOrder: 1,
              width: 150,
              type: "datetime",
              format: "yyyy-MM-dd HH:mm:ss",
              valueFormat: "yyyy-MM-dd HH:mm:ss",    
              rules: [{
                required: false,
                message: "请输入司机签到时间",
                trigger: "blur"
              }]
            },
            {
              label: "发车时间",
              prop: "checkOutTime",
              sortable:true,
              search:true,
              searchOrder: 2,
              width: 150,
              type: "datetime",
              format: "yyyy-MM-dd HH:mm:ss",
              valueFormat: "yyyy-MM-dd HH:mm:ss",    
              rules: [{
                required: false,
                message: "请输入出站核查时间",
                trigger: "blur"
              }]
            },
            {
              label: "安检时间",
              prop: "securityTime",
              sortable:true,
              search:true,
              width: 150,
              type: "datetime",
              format: "yyyy-MM-dd HH:mm:ss",
              valueFormat: "yyyy-MM-dd HH:mm:ss",    
              rules: [{
                required: false,
                message: "请输入安全例检时间",
                trigger: "blur"
              }]
            },
            {
              label: "核载人数",
              prop: "driverSeats",
              width: 90,
              type: "number",
              editable: false,
            },
            {
              label: "实载人数",
              prop: "checkOutCnt",
              type: "number",
              rules: [{
                required: true,
                message: "请输入出站实载人数",
                trigger: "blur"
              }]
            },
            {
              label: "人数(全票)",
              prop: "checkOutCnt1",
              type: "number",
              rules: [{
                required: true,
                message: "请输入出站人数(全票)",
                trigger: "blur"
              }]
            },
            {
              label: "人数(半票)",
              prop: "checkOutCnt2",
              type: "number",
              rules: [{
                required: false,
                message: "请输入出站人数(半票)",
                trigger: "blur"
              }]
            },
            {
              label: "人数(免票)",
              prop: "checkOutCnt3",
              type: "number",
              rules: [{
                required: false,
                message: "请输入出站人数(免票)",
                trigger: "blur"
              }]
            },
            {
              label: "驾驶员",
              prop: "driverName",
              editable: false,
              display: false,
            },
            {
              label: "驾驶员手机",
              prop: "driverPhone",
              width: 120,
              display: false,
              editable: false,
            },
            {
              label: "行车员ID",
              prop: "checkOutUserId",
              hide: true,
              rules: [{
                required: false,
                message: "请输入出站核实人员",
                trigger: "blur"
              }]
            },
            {
              label: "行车员",
              prop: "checkUser",
              editable: false,
            },
            {
              label: "行车员手机",
              prop: "checkPhone",
              width: 120,
              editable: false,
              display: false,
            },
            {
              label: "安检人员",
              prop: "securityUserId",
              hide: true,
              rules: [{
                required: false,
                message: "请输入安全例检人员",
                trigger: "blur"
              }]
            },
            {
              label: "安检员",
              prop: "securityUser",
              editable: false,
            },
            {
              label: "安检员手机",
              prop: "securityPhone",
              width: 120,
              editable: false,
              display: false,
            },
            {
              label: "检查结果",
              prop: "securityContent",
              hide: true,
              rules: [{
                required: false,
                message: "请输入安全例检选项",
                trigger: "blur"
              }]
            },
            // {
            //   label: "站点上车人数（仅备注字段）",
            //   prop: "onCnt",
            //   hide: true,
            //   rules: [{
            //     required: true,
            //     message: "请输入站点上车人数（仅备注字段）",
            //     trigger: "blur"
            //   }]
            // },
            // {
            //   label: "站点下车人数（仅备注字段）",
            //   prop: "offCnt",
            //   hide: true,
            //   rules: [{
            //     required: true,
            //     message: "请输入站点下车人数（仅备注字段）",
            //     trigger: "blur"
            //   }]
            // },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.taskstation_add, false),
          viewBtn: this.vaildData(this.permission.taskstation_view, false),
          delBtn: this.vaildData(this.permission.taskstation_delete, false),
          editBtn: this.vaildData(this.permission.taskstation_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      dateChange (date) {
        this.$message.success(JSON.stringify(date));
      },      
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
